package com.ruiysoft.controller.upload.pic;


import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectResult;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("alioss")
@CrossOrigin
public class ALiOSSController {
    private static final Logger log = LoggerFactory.getLogger(ALiOSSController.class);

    // Endpoint以杭州为例，其它Region请按实际情况填写。
    private String endpoint = "http://oss-cn-shenzhen.aliyuncs.com";
    // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
    private String accessKeyId = "LTAIOlwbYUgNYUBv";
    private String accessKeySecret = "oiItP7Mhorhe2FCg9WoQ7vRoXe7PeR";

    // 创建OSSClient实例。
//    private OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);


    //String localFilePath = "/Users/liudh/Desktop/WechatIMG383.jpeg";
    //默认不指定key的情况下，以文件内容的hash值作为文件名
    String key = null;

    @PostMapping("upload")
    public Object upload(@RequestParam("file") MultipartFile file) {

        log.debug("upload api is invoked..");
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);
        try {
//            String upToken = auth.uploadToken(bucket);
//            Response response = uploadManager.put(file.getInputStream(), key, upToken,null,null);
            PutObjectResult result = ossClient.putObject("xianxiuyigou", "<yourObjectName>", file.getInputStream());


            map.put("success", true);
//            map.put("downloadPath", "http://media.avicyt.com/" + putRet.key);

        } catch (OSSException oe) {

            System.out.println("Caught an OSSException, which means your request made it to OSS, "

                    + "but was rejected with an error response for some reason.");

            System.out.println("Error Message: " + oe.getErrorCode());

            System.out.println("Error Code:       " + oe.getErrorCode());

            System.out.println("Request ID:      " + oe.getRequestId());

            System.out.println("Host ID:           " + oe.getHostId());
            map.put("errorMsg", oe.getMessage());
        } catch (ClientException ce) {

            System.out.println("Caught an ClientException, which means the client encountered "

                    + "a serious internal problem while trying to communicate with OSS, "

                    + "such as not being able to access the network.");

            System.out.println("Error Message: " + ce.getMessage());
            map.put("errorMsg", ce.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            /*

             * Do not forget to shut down the client finally to release all allocated resources.

             */

            ossClient.shutdown();
        }
        return map;
    }




}
